package dongbai.soft.controller;

import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import dongbai.soft.entity.DelivererEntity;
import dongbai.soft.feignclient.DispatchClient;
import dongbai.soft.feignclient.FinanceClient;
import dongbai.soft.service.DelivererService;
import dongbai.soft.utils.Constant;
import dongbai.soft.utils.PageUtils;
import dongbai.soft.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author SingDeer
 * @email
 * @date 2023-06-28 16:09:11
 */
@RestController
@SuppressWarnings("unchecked")
@RequestMapping("")
public class DelivererController {
    @Autowired
    private DelivererService delivererService;

    @Autowired
    private DispatchClient dispatchClient;

    @Autowired
    private FinanceClient financeClient;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestBody Map<String, Object> params) {
        PageUtils page = delivererService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 保存
     */
    @RequestMapping("/add")
    public R save(@RequestBody Map<String, Object> maps) {
        if (delivererService.DUP(maps.get("name").toString(), maps.get("phone").toString())) {
            return R.error(Constant.DEV_DUP);
        }
        delivererService.save(new DelivererEntity(maps));
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/editinfo")
    public R update(@RequestBody Map<String, String> maps) {
        if (delivererService.DUP(maps.get("name"), maps.get("phone"))) {
            return R.error(Constant.DEV_DUP);
        }
        delivererService.update(new UpdateWrapper<DelivererEntity>().eq("DID", maps.get("DID"))
                .set("name", maps.get("name")).set("phone", maps.get("phone")));
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("transfer:deliverer:delete")
    public R delete(@RequestBody Map<String, String> maps) {
        delivererService.update(new UpdateWrapper<DelivererEntity>()
                .eq("DID", maps.get("DID")).set("status", Constant.STATE_N));
        return R.ok();
    }

    /**
     * 结算
     */
    @RequestMapping("/close")
    //@RequiresPermissions("transfer:deliverer:delete")
    public R sign(@RequestBody Map<String, Object> maps) {
        financeClient.rclose(maps);
        List<Map<String, String>> datas = (List<Map<String, String>>) maps.get("list");
        List<String> oids = new ArrayList<>();
        for (Map<String, String> data : datas) {
            oids.add(data.get("OID"));
        }
        Map<String, Object> map = new HashMap<>();
        map.put("OIDS", oids);
        map.put("isend", Constant.OPAID);
        dispatchClient.setStatus(map);
        return R.ok();
    }

    /**
     * 分配配送员
     */
    @RequestMapping("/delivery")
    //@RequiresPermissions("transfer:deliverer:delete")
    public R delievry(@RequestBody Map<String, String> maps) {
        delivererService.update(new UpdateWrapper<DelivererEntity>().eq("DID", maps
                .get("DID")).setSql("sum = sum+1"));
        dispatchClient.deliverGood(maps);

        return R.ok();
    }

}
